using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
namespace Services
{

    public partial class jobService
    {
       readonly IjobEntity entity;

       public jobService(IjobEntity entity)
       {
           this.entity = entity;

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("id").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.job)_model, Table.job.ToString());
               }
               else
               {
                   return entity.Update((CoreData.job)_model, Table.job.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.job GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.job.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.job> GetList()
       {
           try
           {
               return entity.GetAll(Table.job.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.job> GetListByLINQ(Func<CoreData.job, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.job.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.job GetOneByLINQ(Func<CoreData.job, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.job.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.job> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.job.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.job)_model, Table.job.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion

       public virtual string LoadMenuJob() {
           string StrMenu = "";
           var lstParent = this.GetListByLINQ(c => c.parentid == 0);
           if (lstParent!=null&&lstParent.Count > 0)
           {
               foreach (var item in lstParent)
               {
                   StrMenu += "<li><a href=" + "" + item.link + "" + ">" + @item.name + "</a>";
                   var lstChild = this.GetListByLINQ(c => c.parentid == item.id);
                   if (lstChild!=null&&lstChild.Count > 0)
                   {
                       StrMenu += "<ul class='child-toggle'>";
                       foreach (var objChild in lstChild)
                       {
                           StrMenu += "<li><a href=" + "" + objChild.link + "" + ">" + objChild.name + "</a>";
                       }
                       StrMenu += "</ul>";
                   }
                   else
                   {
                       StrMenu += "</li>";
                   }
               }
           }
           return StrMenu;
       }

        #region Other Method



        #endregion 
    
    }
         
}
